{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# TensorFlow 的基本结构",
   "id": "1775d09a3bd85785"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-27T13:22:51.047661Z",
     "start_time": "2025-03-27T13:22:51.041152Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import tensorflow._api.v2.compat.v1 as tf\n",
    "tf.disable_v2_behavior()"
   ],
   "id": "afc403eb5c30a669",
   "outputs": [],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-27T11:51:20.305974Z",
     "start_time": "2025-03-27T11:51:20.297461Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 原生Python加法运算\n",
    "a = 2\n",
    "b = 3\n",
    "c = a + b\n",
    "print(c)"
   ],
   "id": "b553f49a4ae34cbc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-27T12:05:24.380483Z",
     "start_time": "2025-03-27T12:05:24.357165Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# TensorFlow加法运算\n",
    "a = tf.constant(2)\n",
    "b = tf.constant(3)\n",
    "# c = a + b\n",
    "c = tf.add(a, b)\n",
    "print(c)"
   ],
   "id": "5be1ab1ea190d636",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(5, shape=(), dtype=int32)\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-27T13:22:56.287463Z",
     "start_time": "2025-03-27T13:22:56.102543Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 开启会话\n",
    "# 计算a和b的和，并将结果输出到屏幕\n",
    "with tf.Session() as sess:\n",
    "    result = sess.run(c)\n",
    "    print(result)"
   ],
   "id": "ec87060de954af61",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-27T11:58:53.754609Z",
     "start_time": "2025-03-27T11:58:53.740510Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 在TensorFlow 2.x中，Eager Execution默认开启，所以不需要显式开启会话\n",
    "# 直接打印结果即可\n",
    "print(c.numpy())"
   ],
   "id": "89296eecc6167d13",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 图的演示",
   "id": "7786953fb1de943b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-27T13:23:00.591103Z",
     "start_time": "2025-03-27T13:23:00.568763Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 开启会话\n",
    "# 定义两个常量张量\n",
    "a = tf.constant(2)\n",
    "b = tf.constant(3)\n",
    "# 定义一个加法操作\n",
    "c = tf.add(a, b)\n",
    "# 查看默认图\n",
    "# 方法一：调用方法tf.get_default_graph()\n",
    "default_graph = tf.get_default_graph()\n",
    "print(default_graph)"
   ],
   "id": "6c69ab0a18c02072",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<tensorflow.python.framework.ops.Graph object at 0x00000217638193C0>\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-27T13:23:04.716099Z",
     "start_time": "2025-03-27T13:23:04.709494Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 方法二：直接打印c的图属性\n",
    "print(c.graph)"
   ],
   "id": "385f1cc1da2dafdf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<tensorflow.python.framework.ops.Graph object at 0x00000217638193C0>\n"
     ]
    }
   ],
   "execution_count": 23
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
